<template>
  <div class="home">
    <div class="container">
      <el-form :inline="true" :model="form" :rules="rules" ref="form" class="form" label-width="120px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入图书名称"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述"></el-input>
        </el-form-item>
        <el-form-item label="出版日期">
          <el-date-picker v-model="form.publish_date" type="date" placeholder="请选择出版日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者"></el-input>
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社"></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-cascader v-model="form.category" :props="{ value: 'name', label: 'name' }" :options="categories">
          </el-cascader>
        </el-form-item>
        <el-form-item label="标准码" prop="book_no">
          <el-input v-model="form.book_no" placeholder="请输入标准码"></el-input>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <el-input v-model="form.cover" placeholder="请输入封面"></el-input>
        </el-form-item>
        <el-form-item label="所需积分" prop="score">
          <el-input-number v-model="form.score" :min="1" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="库存" prop="nums">
          <el-input-number v-model="form.nums" :min="1" label="描述文字"></el-input-number>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="save">添加</el-button>

    </div>
  </div>
</template>

<script>

export default {
  name: 'AddBook',
  data() {
    return {
      form: {
        score:1,
        nums:1
      },
      rules: {
        name: [
          { required: true, message: '请输入图书名称', trigger: 'blur' },
        ],
        description: [
          { required: true, message: '请输入描述', trigger: 'blur' },
        ],
        publish_date: [
          { required: true, message: '请输入出版日期', trigger: 'blur' },
        ],
        author: [
          { required: true, message: '请输入作者', trigger: 'blur' },
        ],
        publisher: [
          { required: true, message: '请输入出版社', trigger: 'blur' },
        ],
        category: [
          { required: true, message: '请输入分类', trigger: 'blur' },
        ],
        book_no: [
          { required: true, message: '请输入标准码', trigger: 'blur' },
        ],
        cover: [
          { required: true, message: '请输入封面', trigger: 'blur' },
        ],
        score: [
          { required: true, message: '请输入所需积分', trigger: 'blur' },
        ],
        nums: [
          { required: true, message: '请输入数量', trigger: 'blur' },
        ],
      },
      categories: [],
    }
  },
  created() {
    this.request.get("/api/category/list").then(res => {
      this.categories = res.data;
    })
  },
  methods: {
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.category = this.form.category.join('>');
          this.request.post("/api/book/save", this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success("添加成功");
            } else {
              this.$notify.error("添加失败");
            }
            this.form = {};
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>


<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;

  .container {
    width: 800px;
    height: 500px;

    margin-top: 80px;
    background-color: #ffffff;
    border-radius: 25px;
    box-shadow: 0 3px 5px #ccc;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;

    .form {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
    }
  }
}
</style>